
const state = {
    view: null,
    alert: null,
    msg: 0,
	messageRouter: ''
}

const mutations = {
    close(state) {
        state.view.close()
        state.view = null
        state.alert.close();
        state.alert = null
    },
    // 显示按钮
    show(state,router) {
		if(router) state.messageRouter = router;
        if (!state.view) {
            create(state)
            return
        }
        state.view.show();
        if (!state.alert) {
            create(state)
            return
        } 
        state.alert.show();
    },
    // 隐藏按钮
    hide(state) {
        state.view && state.view.hide()
        state.alert && state.alert.hide() 
    },
    changeMsg(state,msg) {
        state.msg = msg;
        // create(state)
        let richtext = `<img src="${msg.url}" width="${getpx(100)}" height="${getpx(100)}"></img>
        `;
        state.alert && state.alert.drawRichText(richtext, {top:getpx(34),left:getpx(48),width:'100%',height:'wrap_content'},{align:'left'},'num');
        let len = msg.tips.replace(/[^\x00-\xff]/g, "00").length,
        byteL = 0, msgL = 0, msgContent = msg.tips; 

        if(len>34 || msg.tips.length>18){
            for(let i=0; i<msg.tips.length; i++){
                let L = msg.tips[i].replace(/[^\x00-\xff]/g, "00").length
                byteL += L
                if(msg.tips.charCodeAt(i)>=64 && msg.tips.charCodeAt(i)<=90){
                    byteL += 1
                }else if(msg.tips.charCodeAt(i)>=97 && msg.tips.charCodeAt(i)<=122){
                    byteL += 0.5
                }
                if(byteL<= 34){
                    msgL = i+1
                }
            }
            msgContent = msg.tips.slice(0, msgL)+ (msgL==msg.tips.length?'':'...')
        }
        
        let richtext2 = `<text class="name" style="font-size: ${getpx(28)};font-weight: bold;">${msg.name}</text><br/><br/><view class="app-msg-tips" style="width: ${getpx(428)};"><text class="name">${msgContent}</text></view>`
        state.alert && state.alert.drawRichText(richtext2, {top:getpx(34),left:getpx(172),width:'100%',height:'wrap_content'},{align:'left'}, 'text');
        // console.log(state.msg, state.alert,62)
    }
}
function getpx(rpx){
	
	return rpx / 750 * uni.getSystemInfoSync().windowWidth + 'px';
}
function create(state){
    uni.getSystemInfo({
        success:(info)=>{
            //初始化存一些设备相关的参数
            try {
                uni.setStorageSync('screenHeight', info.screenHeight);
                uni.setStorageSync('screenWidth', info.screenWidth);
            } catch (e) {
                // error
            }
        }
    })
	
    // #ifdef APP-PLUS
    state.view = new plus.nativeObj.View('bottom-btn', {
        top: getpx(100),
        left: getpx(75),
        height: getpx(168),
        width: '80%'
    },
    [{
        tag: 'rect',
        id: 'rect',
        rectStyles: {color:'#ffffff', radius: '10px'},
        position:{top: '0px', left: '0px', width: '100%', height: '100%'}
    }]
    );
    state.alert = new plus.nativeObj.View('num', {
        left: getpx(48),
        top: getpx(100),
        height: getpx(168),
        width: getpx(600),
        backgroundColor: 'rgba(0,0,0,0)'
    }
    );	
    // http://192.168.2.21/upload/example/1657161928869_779.jpg
    // let richtext = `
    //     <img src="/static/u68.png" width="${getpx(100)}" height="${getpx(100)}"></img><text class="name">名字</text><br/>
    //     <text class="name">[${state.msg}条待审核任务]</text>`;
    // // 绘制文本
    // state.alert.drawRichText(richtext, {top:getpx(34),left: getpx(75),width:getpx(600),height:'wrap_content'},{align:'left'},'num');
    state.alert.setTouchEventRect({top:'0',left:'0',width:'100%',height:getpx(168)});	
    // 监听点击事件
    state.alert.addEventListener("click", (e) => {
        let path = state.messageRouter
        if(path.indexOf('parmas=tabBar')!=-1){
            uni.switchTab({
                url: path,
                success: ()=>{
                    uni.$emit('Mhide')
                }
            })
        }else{
            if(path.indexOf('params=redirect')!=-1){
                uni.redirectTo({
                    url: path,
                    success: ()=>{
                        uni.$emit('Mhide')
                    }
                })
            }else{
                uni.navigateTo({
                    url: path,
                    success: ()=>{
                        uni.$emit('Mhide')
                    }
                })
            }
            // uni.redirectTo
            
        }
    }, false);
    // #endif
}

const getters = {
    msg: state => state.msg
}

export default {
    namespaced: true,
    state,
    mutations,
    getters
}
